from neo4j import GraphDatabase
from typing import Any
from backend.utils.logger import logger

class Neo4jDriver:
    def __init__(self, uri: str, user: str, password: str):
        self.driver = GraphDatabase.driver(uri, auth=(user, password))
        logger.info("Neo4j driver initialized.")

    def close(self):
        self.driver.close()
        logger.info("Neo4j driver closed.")

    def query(self, cypher: str, parameters: dict = None) -> Any:
        logger.info(f"Executing Cypher: {cypher} | Params: {parameters}")
        try:
            with self.driver.session() as session:
                result = session.run(cypher, parameters or {})
                data = [record.data() for record in result]
                logger.info(f"Query returned {len(data)} records.")
                return data
        except Exception as e:
            logger.error(f"Neo4j query error: {e}")
            raise

# 单例
neo4j_driver = Neo4jDriver(
    uri="bolt://localhost:7687",
    user="neo4j",
    password="123456789"
) 